package com.czb.servlet;


import com.czb.service.ActivityService;
import com.czb.service.ActivityServiceImpl;
import com.czb.util.PageBean;
import com.czb.util.ResultJson;
import com.czb.util.Utils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

@WebServlet("/ActivityServlet")
public class ActivityServlet extends HttpServlet {
    ActivityService activityService = new ActivityServiceImpl();
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            request.setCharacterEncoding("utf-8");
            response.setCharacterEncoding("utf-8");
            String action = request.getParameter("action");
            if (action.equals("showAll")){
                showAll(request,response);
            }else if (action.equals("add")){
                add(request,response);
            }else if (action.equals("toadd")){
                toadd(request,response);
            }else if (action.equals("showById")){
                showById(request,response);
            }else  if (action.equals("update")){
                update(request,response);
            }else if (action.equals("del")){
                del(request,response);
            }
    }

    private void toadd(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.getRequestDispatcher("/WEB-INF/jsp/add.jsp").forward(request,response);
    }

    private void del(HttpServletRequest request, HttpServletResponse response) throws ServletException,IOException{
        Integer a_id = Integer.valueOf(request.getParameter("a_id"));
        Object[] objects = {a_id};
        String sql = "delete from activity where a_id =?";
        int i = activityService.del(sql,objects);
        request.getRequestDispatcher("/ActivityServlet?action=showAll").forward(request,response);
    }

    private void update(HttpServletRequest request, HttpServletResponse response) throws ServletException,IOException{
        Integer a_id = Integer.parseInt(request.getParameter("a_id"));
        String a_subject = request.getParameter("a_subject");
        String a_content = request.getParameter("a_content");
        String a_site = request.getParameter("a_site");
        Integer a_cost = Integer.parseInt(request.getParameter("a_cost"));
        String a_organizer = request.getParameter("a_organizer");
        String a_taketime = request.getParameter("a_taketime");
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date time = null;
        try {
            time = sdf.parse(a_taketime);
            Object[] objects = {a_subject,a_content,a_site,a_cost,a_organizer,a_taketime,a_id};
            String sql = "update activity set a_subject = ?,a_content=?,a_site=?,a_cost=?,a_organizer=?,a_taketime=? where a_id=?";
            int i = activityService.update(sql,objects);
            ResultJson success = ResultJson.success(i);
            Utils.returnJson(success,response);
        } catch (ParseException e) {
            e.printStackTrace();
        }

    }

    private void showById(HttpServletRequest request, HttpServletResponse response) throws ServletException,IOException{
        String sql ="";
        Integer a_id = Integer.parseInt(request.getParameter("a_id"));
        sql = "select * from activity where a_id = ?";
        Map map = activityService.showById(sql,a_id);
        ResultJson success = ResultJson.success(map);
        Utils.returnJson(success,response);
    }

    //添加
    private void add(HttpServletRequest request, HttpServletResponse response)throws ServletException,IOException {
        String a_subject = request.getParameter("a_subject");
        String a_content = request.getParameter("a_content");
        String a_site = request.getParameter("a_site");
        Integer a_cost = Integer.parseInt(request.getParameter("a_cost"));
        String a_organizer = request.getParameter("a_organizer");
        String a_taketime = request.getParameter("a_taketime");
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date time = null;
        try {
            time = sdf.parse(a_taketime);
            Object[] objects = {a_subject,a_content,a_site,a_cost,a_organizer,a_taketime};
            String sql = "insert into activity values(null,?,?,?,?,?,?)";
            int i = activityService.add(sql,objects);
            ResultJson success = ResultJson.success(i);
            Utils.returnJson(success,response);
            //request.getRequestDispatcher("/ActivityServlet?action=showAll").forward(request,response);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        }
//查询全部
private void showAll(HttpServletRequest request, HttpServletResponse response) throws ServletException,IOException{
    Integer page = request.getParameter("page") != null ? Integer.valueOf(request.getParameter("page")) : 1;
    Integer limit = request.getParameter("limit") != null ? Integer.valueOf(request.getParameter("limit")) : 10;
        String a_subject = request.getParameter("a_subject")!=null?"%"+request.getParameter("a_subject")+"%":"%%";

        String sql = "select * from activity where 1 = 1 and a_subject like ?";
        PageBean<Map> pageBean = activityService.findAll(page,limit,sql,a_subject);
        System.out.println("aaaaaaaaaaaaaaaaaaaaa");
       ResultJson success = ResultJson.success(pageBean);
      Utils.returnJson(success,response);
       // request.getSession().setAttribute("list",list);
       // request.getRequestDispatcher("/WEB-INF/jsp/home.jsp").forward(request,response);
        }

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request,response);
        }
        }
